﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel.Design;

namespace Srvtools
{
    public partial class frmInfoDataGridViewEditor : Form
    {
        InfoDataGridView dataGridViewCopy;
        IDesignerHost designHost;
        
        public frmInfoDataGridViewEditor(InfoDataGridView idgv,IDesignerHost host)
        {
            InitializeComponent();
            dataGridViewCopy = idgv;
            designHost = host;
            InitialGridView();
        }

        private void InitialGridView()
        {
            int colNum = dataGridViewCopy.Columns.Count;
            DataGridViewColumn[] dgvc = new DataGridViewColumn[colNum];
            
            for (int i = 0; i < colNum; i++)
            {
                dgvc[i] = new DataGridViewColumn();
                dgvc[i].Width = dataGridViewCopy.Columns[i].Width;
                dgvc[i].HeaderText = dataGridViewCopy.Columns[i].HeaderText;
                dgvForAdjust.Columns.Add(dgvc[i]);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {

            IComponentChangeService ComponentChangeService =
              this.designHost.GetService(typeof(IComponentChangeService)) as IComponentChangeService;

            // Columns
            PropertyDescriptor descColumns = TypeDescriptor.GetProperties(this.dataGridViewCopy)["Columns"];
            ComponentChangeService.OnComponentChanging(this.dataGridViewCopy, descColumns);

            int colNum = dataGridViewCopy.Columns.Count;
            for (int i = 0; i < colNum; i++)
            {
                dataGridViewCopy.Columns[i].Width = dgvForAdjust.Columns[i].Width;
            }
            ComponentChangeService.OnComponentChanged(this.dataGridViewCopy, descColumns, null, null);

            this.Close();
        }
    }
}